package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.LanUserPraiseReq;
import com.cloud.lan.api.dto.resp.LanUserResp;
import com.cloud.lan.config.cache.LoadingCacheConfiguration;
import com.cloud.lan.dao.LanUserPraiseMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanUserPraiseDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanUserPraiseService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 用户点赞表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-05-21
 */
@Slf4j
@Service
public class LanUserPraiseServiceImpl extends ServiceImpl<LanUserPraiseMapper, LanUserPraiseDO> implements ILanUserPraiseService {

    @Resource
    LoadingCacheConfiguration cacheConfiguration;
    @Resource
    LanUserPraiseMapper lanUserPraiseMapper;

    @Override
    public BaseResponse usPraise(LanUserPraiseReq req) {
        log.info("#新增 用户点赞 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getBeUserId()), String.valueOf(req.getEvaluateId())},
                new String[]{"beUserId", "evaluateId"}, LanError.ILLEGAL_PARAMS.getCode());
        LanUserResp lanUserResp = cacheConfiguration.userInfo.getUnchecked(req.getToken());
        LanUserPraiseDO lanUserPraiseDO = new LanUserPraiseDO();
        BeanUtil.copyProperties(req, lanUserPraiseDO);
        lanUserPraiseDO.setUserId(lanUserResp.getUserId());
        LambdaQueryWrapper<LanUserPraiseDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanUserPraiseDO::getUserId, lanUserPraiseDO.getUserId());
        qw.eq(LanUserPraiseDO::getBeUserId, lanUserPraiseDO.getBeUserId());
        qw.eq(LanUserPraiseDO::getEvaluateId, req.getEvaluateId());
        LanUserPraiseDO userPraiseDO = lanUserPraiseMapper.selectOne(qw);
        if (ObjectUtil.isNotEmpty(userPraiseDO)) {
            LambdaUpdateWrapper<LanUserPraiseDO> luw = new LambdaUpdateWrapper<>();
            luw.set(LanUserPraiseDO::getCancel, userPraiseDO.getCancel().equals(ONE.getKey()) ? ZERO.getKey() : ONE.getKey());
            luw.set(LanUserPraiseDO::getUpdateTime, DateUtil.now());
            luw.eq(LanUserPraiseDO::getId, userPraiseDO.getId());
            lanUserPraiseMapper.update(new LanUserPraiseDO(), luw);
            return BaseResponse.success();
        }
        lanUserPraiseMapper.insert(lanUserPraiseDO);
        return BaseResponse.success();
    }
}
